package com.changgou.config.service.impl;

import com.changgou.config.dao.AreaMapper;
import com.changgou.config.pojo.Area;
import com.changgou.config.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;


    /**
     * 省市区嵌套
     * @return
     */
    @Override
    public List<Area> findAllArea() {
        List<Area> areas = areaMapper.selectAll();
        List<Area> arrayList = new ArrayList<>();
        for (Area area : areas) {
            if (area.getParentid()!=null&&area.getParentid() == 1 ) {
                List<Area> arealist1 = new ArrayList<>();
                for (Area area1 : areas) {
                    if (area1.getParentid()!=null&&area1.getParentid().equals(area.getId()) ) {
                        List<Area> arealist2 = new ArrayList<>();
                        for (Area area2 : areas) {
                            if ( area2.getParentid()!=null&&area2.getParentid().equals(area1.getId())  ) {
                                arealist2.add(area2);

                            }
                        }
                        area1.setAreaList(arealist2);
                        arealist1.add(area1);
                    }
                }
                area.setAreaList(arealist1);
                arrayList.add(area);
            }

        }
        return arrayList;
    }
}
